import { _decorator, Color, Component, EditBox, game, Node, Vec3 } from 'cc';
import { GLOBALUtil } from './GlobalUtil';
const { ccclass, property } = _decorator;

enum LoginState {
    Login,  //登录
    Register,   //注册
    Forget,  //忘记密码
    Change, //修改密码
}
@ccclass('LoginManager')
export class LoginManager extends Component {
    @property({type: Node, tooltip: "登陆面板"})
    private LoginPanel: Node | null = null;
    @property({type: Node, tooltip: "注册面板"})
    private RegisterPanel: Node | null = null;
    @property({type: Node, tooltip: "忘记密码"})
    private ForgetPanel: Node | null = null;

    private MessageNode: Node;
    private state: LoginState | null = null;
    start() {

    }

    update(deltaTime: number) {
        
    }

    onClickExitBtn() {
        game.end();
    }
    
    onClickOpenUI(event: Event, data: string) {
        switch (data) {
            case "Login":
                this.state = LoginState.Login;
                this.LoginPanel.active = true;
                break;
            case "Register":
                this.state = LoginState.Register;
                this.RegisterPanel.active = true;
                break;
            case "Forget":
                this.state = LoginState.Forget;
                this.LoginPanel.active = false;
                this.ForgetPanel.active = true;
                break;
        }
    }
    onClickLogin(event: Event) {
        const NodeTarget = event.target as unknown as Node;
        const address = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxUserName").getComponent(EditBox).string
        const password = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxPassword").getComponent(EditBox).string

        if (!GLOBALUtil.validateEmail(address)) {
            if (this.MessageNode) this.MessageNode.destroy();
            this.messageTip(true, "邮箱格式有误！", this.LoginPanel, new Vec3(50, 950, 0))
        } else {
            if (this.MessageNode) this.MessageNode.destroy();
        }
    }

    onClickRegister(event: Event) {
        const NodeTarget = event.target as unknown as Node;
        const address = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxUserName").getComponent(EditBox).string
        const password = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxPassword").getComponent(EditBox).string

        if (!GLOBALUtil.validateEmail(address)) {
            if (this.MessageNode) this.MessageNode.destroy();
            this.messageTip(true, "邮箱格式有误！", this.RegisterPanel, new Vec3(50, 950, 0))
        } else {
            if (this.MessageNode) this.MessageNode.destroy();
        }
    }

    onClickForget(event: Event) {
        const NodeTarget = event.target as unknown as Node;
        const address = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxUserName").getComponent(EditBox).string
        const password = NodeTarget.parent.getChildByPath("AccountNode/UserNode/EditBoxPassword").getComponent(EditBox).string

        if (!GLOBALUtil.validateEmail(address)) {
            if (this.MessageNode) this.MessageNode.destroy();
            this.messageTip(true, "邮箱格式有误！", this.ForgetPanel, new Vec3(50, 950, 0))
        } else {
            if (this.MessageNode) this.MessageNode.destroy();
        }
    }

    onClickGetCode(event: Event) {
        const NodeTarget = event.target as unknown as Node;
        const address = NodeTarget.parent.getChildByName("EditBoxUserName").getComponent(EditBox).string

        if (!GLOBALUtil.validateEmail(address)) {
            if (this.MessageNode) this.MessageNode.destroy();
            this.messageTip(true, "邮箱格式有误！", NodeTarget.parent, new Vec3(50, 950, 0))
        } else {
            if (this.MessageNode) this.MessageNode.destroy();
        }
    }
    onCloseBG(evnet: Event) {
        let NodeTarget = evnet.target as unknown as Node;
        NodeTarget.parent.active = false;
    }

    private messageTip(isClose: boolean, msg:string, parent: Node, targetlPos: Vec3, initialPos = new Vec3(-300, 950, 0), color: Color = new Color(255,255,255,255)) {
        let promise = GLOBALUtil.LoadMessage(isClose, msg, parent, targetlPos)
        promise.then((node: Node) => {
            this.MessageNode = node;
        })
    }
}


